﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Book
{
    public partial class Reg : Form
    {
        public Reg()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 确认注册信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            var userName = txt_UserName.Text;
            var pwd = txt_Pwd.Text;

            var nullUserName = string.IsNullOrEmpty(userName);
            var nullPwd = string.IsNullOrEmpty(pwd);

            var isUserName = string.Format("select Username from Users where UserName='{0}'", userName);
            var dt = DbHelper.GetSelect(isUserName);

            if (!nullUserName && !nullPwd && dt.Rows.Count==0)
            {
                string sql = string.Format("insert into Users(Username,Password) values('{0}','{1}')", userName, pwd);
                var res = DbHelper.GetAddOrDelOrUp(sql);
                if (res>=1)
                {
                    MessageBox.Show("注册成功！", "消息提示");
                    this.Close();
                    Form1 form1 = new Form1();
                    form1.Show();
                }
            }
            else if(nullUserName)
            {
                MessageBox.Show("用户名不能为空！", "消息提示");
            }
            else if (nullPwd)
            {
                MessageBox.Show("密码不能为空！", "消息提示");
            }
            else if (dt.Rows.Count == 1)
            {
                MessageBox.Show("用户名已存在！", "消息提示");
            }
            else
            {
                MessageBox.Show("注册失败！", "消息提示");
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 form1 = new Form1();
            form1.Show();
        }
    }
}
